import java.util.ArrayList;

public class TextBox
{
	public int x;
	public int y;
	public int width = 130;
	public String text;
	public String emptyTextBox;
	public ScrollBar scrollBar;
	public boolean active;
	public ArrayList<Object> objectsToShow;
	public TextBoxButtons textBoxButtons;
	public ArrayList<Object> objectsSelected;

	public float buttonTimer;
	public int heightVal;
	public int suggestionHeight;

	public  class TextBoxButtons{
		public ArrayList<Button> suggestionButtons;
		public ArrayList<Object> shapes;

		public TextBoxButtons(){
			suggestionButtons = new ArrayList<Button>();
			shapes = new ArrayList<Object>();
		}
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////
	public TextBox(String emptyTextBox,ArrayList<Object> objectsSelected)
	{
		text = "";
		scrollBar = new ScrollBar();
		active=false;
		this.emptyTextBox = emptyTextBox;
		//shapesToShow = GLOBAL.shapes;
		textBoxButtons = new TextBoxButtons();
		this.objectsSelected = objectsSelected;
		updateButtons();
	}

	public void draw()
	{
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);

		GLOBAL.processing.fill(GLOBAL.colorBackground);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.rect(x, y, width, 30);

		GLOBAL.processing.fill(GLOBAL.textColor);	
		//GLOBAL.processing.textFont(GLOBAL.processing.plotFont, 14);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		suggestionHeight = 130;

		if(active){
			if(buttonTimer==(float)0.0){
				scrollBar.val=0;
				buttonTimer = GLOBAL.timer;
				heightVal=0;
			}
			heightVal = (int)GLOBAL.processing.lerp(heightVal,suggestionHeight,(float) 0.3);
			drawSuggestions();
		}
		else{if(buttonTimer!=(float)0.0){heightVal=suggestionHeight;}
		GLOBAL.processing.text(emptyTextBox, x + 5, y + 20);
		heightVal = (int)GLOBAL.processing.lerp(heightVal,0,(float) 0.3);
		if(heightVal!=0)
		drawSuggestions();
		buttonTimer=(float)0.0;

		}

	}
	
	public void drawSuggestions(){
		GLOBAL.processing.text(text, x + 5, y + 20);
		GLOBAL.processing.fill(44,160);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.rect(x, y+30, width, heightVal);


		scrollBar.width = 15;
		scrollBar.x =  x +width-15;
		scrollBar.y = y+30;
		scrollBar.height = heightVal-4;
		if(heightVal>scrollBar.y-y)
		scrollBar.draw();  
		int fakeSize =10; //TODO change this
		scrollBar.size = 1/(float)(fakeSize-3);
		showButtons();
	}
	
	

	
	
	
	public void keyTyped()
	{
		if(GLOBAL.processing.key==GLOBAL.processing.ENTER){

			if(objectsToShow.size()>0){
				Object o =objectsToShow.get(0);
				if(objectsSelected.contains(o)){
					objectsSelected.remove(o);
				}
				else{
					objectsSelected.add(o);
				}
				this.active=false;
				text = new String();
			}


		}
		else if(GLOBAL.processing.key == 8)
		{
			if(text.length() > 0) text = text.substring(0, text.length() - 1);
		}
		else if(text.length()>16){
			text = text.substring(0, text.length() - 1);
			text += GLOBAL.processing.key;
		}
		else
		{
			text += GLOBAL.processing.key;
		}

		updateButtons();

	}


	public boolean mouseOver()
	{
		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y && GLOBAL.processing.mouseY < y + 30) return true;
		return false;
	}

	public boolean mouseOverSuggestions(){

		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y+30 && GLOBAL.processing.mouseY < y + 160+50)return true;
		return false;
	}

	//show  buttons in the suggestions
	private void showButtons() {

		float j = Main.map (scrollBar.val,0,1,0,objectsToShow.size()-3);
		if(j<0)
			j=0;
		for(int i=(int)j;i<(int)j+5 && i<textBoxButtons.suggestionButtons.size()&&i-(int)j<+heightVal/28-0.3;i++){
			Button b = textBoxButtons.suggestionButtons.get(i);
			if(b.state){

				if(objectsToShow.size()>i){
		Object o =objectsToShow.get(i);
		if(objectsSelected.contains(o)){
			objectsSelected.remove(o);
		}
		else{
			objectsSelected.add(o);
		}
				
				
					this.active=false;
					text = new String();
				}

				updateButtons();
				break;
			}else{
				b.y=y+30+(i-(int)j)*25;
				b.draw();
			}
		}		
	}
	
	
	public void updateButtons(){
//		objectsToShow = new ArrayList<Object>();
//		for(int i=0;i<GV.shapes.size();i++){
//			if(GV.shapes.get(i).getName().toLowerCase().startsWith(text.toLowerCase())){
//				objectsToShow.add(GV.shapes.get(i));
//				//System.out.println("I add "+Utils.characters.get(i).getName());
//			}
//		}
//		textBoxButtons.suggestionButtons= new ArrayList<Button>();
//		for(int i=0;i<objectsToShow.size();i++){
//			Button b = new Button(objectsToShow.get(i).getName(),x+5,0);
//			sb.shapesSuggestionButtons.add(b);
//			sb.shapes.add(objectsToShow.get(i));
//		}

	}
	
}